{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.283118900Z",
     "start_time": "2025-08-25T07:15:15.911332500Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "ts = pd.Timestamp('2023-06-15 14:30:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Timestamp:2023-06-15 14:30:00\n"
     ]
    }
   ],
   "source": [
    "print(f'Timestamp:{ts}')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.285117800Z",
     "start_time": "2025-08-25T07:15:15.929322400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "DatetimeIndex:\n",
      "DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04',\n",
      "               '2023-01-05'],\n",
      "              dtype='datetime64[ns]', freq='D')\n"
     ]
    }
   ],
   "source": [
    "# 创建DatetimeIndex\n",
    "date_range = pd.date_range('2023-01-01', periods=5, freq='D')\n",
    "print(f\"\\nDatetimeIndex:\\n{date_range}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.294111700Z",
     "start_time": "2025-08-25T07:15:15.963302500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Period: 2023-06\n"
     ]
    }
   ],
   "source": [
    "# 创建Period\n",
    "p = pd.Period('2023-06')\n",
    "print(f\"\\nPeriod: {p}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.295112400Z",
     "start_time": "2025-08-25T07:15:15.977295900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "PeriodIndex:\n",
      "PeriodIndex(['2023-01', '2023-02', '2023-03'], dtype='period[M]')\n"
     ]
    }
   ],
   "source": [
    "# 创建PeriodIndex\n",
    "period_range = pd.period_range('2023-01', periods=3, freq='M')\n",
    "print(f\"\\nPeriodIndex:\\n{period_range}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.296111700Z",
     "start_time": "2025-08-25T07:15:16.014273100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "从字符串创建:\n",
      "DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03'], dtype='datetime64[ns]', freq=None)\n"
     ]
    }
   ],
   "source": [
    "# 从字符串创建时间序列\n",
    "dates = ['2023-01-01', '2023-01-02', '2023-01-03']\n",
    "ts_list = pd.to_datetime(dates)\n",
    "print(f\"\\n从字符串创建:\\n{ts_list}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.296111700Z",
     "start_time": "2025-08-25T07:15:16.037259900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "时间序列DataFrame:\n",
      "            temperature   humidity\n",
      "2023-01-01           21  71.187640\n",
      "2023-01-02           18  63.874006\n",
      "2023-01-03           27  57.833310\n",
      "2023-01-04           29  43.998997\n",
      "2023-01-05           25  58.369956\n"
     ]
    }
   ],
   "source": [
    "# 创建带时间索引的DataFrame\n",
    "np.random.seed(42)\n",
    "df = pd.DataFrame({\n",
    "    'temperature': np.random.randint(15, 30, 5),\n",
    "    'humidity': np.random.uniform(40, 80, 5)\n",
    "}, index=pd.date_range('2023-01-01', periods=5, freq='D'))\n",
    "print(f\"\\n时间序列DataFrame:\\n{df}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.296111700Z",
     "start_time": "2025-08-25T07:15:16.068243400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "            temperature   humidity\n2023-01-01           21  71.187640\n2023-01-02           18  63.874006\n2023-01-03           27  57.833310\n2023-01-04           29  43.998997\n2023-01-05           25  58.369956",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>temperature</th>\n      <th>humidity</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2023-01-01</th>\n      <td>21</td>\n      <td>71.187640</td>\n    </tr>\n    <tr>\n      <th>2023-01-02</th>\n      <td>18</td>\n      <td>63.874006</td>\n    </tr>\n    <tr>\n      <th>2023-01-03</th>\n      <td>27</td>\n      <td>57.833310</td>\n    </tr>\n    <tr>\n      <th>2023-01-04</th>\n      <td>29</td>\n      <td>43.998997</td>\n    </tr>\n    <tr>\n      <th>2023-01-05</th>\n      <td>25</td>\n      <td>58.369956</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.317099600Z",
     "start_time": "2025-08-25T07:15:16.101224Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "按日期索引:\n",
      "temperature    27.00000\n",
      "humidity       57.83331\n",
      "Name: 2023-01-03 00:00:00, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 按日期字符串索引\n",
    "print(\"\\n按日期索引:\")\n",
    "print(df.loc['2023-01-03'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.318099400Z",
     "start_time": "2025-08-25T07:15:16.134205100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "日期范围索引:\n",
      "            temperature   humidity\n",
      "2023-01-03           27  57.833310\n",
      "2023-01-04           29  43.998997\n",
      "2023-01-05           25  58.369956\n"
     ]
    }
   ],
   "source": [
    "# 按日期范围索引\n",
    "print(\"\\n日期范围索引:\")\n",
    "print(df['2023-01-03':'2023-01-05'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.320098200Z",
     "start_time": "2025-08-25T07:15:16.150195900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [
    "# 创建小时级数据\n",
    "hourly_data = pd.DataFrame(\n",
    "    {'value': np.random.rand(24)},\n",
    "    index=pd.date_range('2023-01-01', periods=24, freq='h')\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.321097500Z",
     "start_time": "2025-08-25T07:15:16.186175200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "                        value\n2023-01-01 00:00:00  0.333709\n2023-01-01 01:00:00  0.142867\n2023-01-01 02:00:00  0.650888\n2023-01-01 03:00:00  0.056412\n2023-01-01 04:00:00  0.721999\n2023-01-01 05:00:00  0.938553\n2023-01-01 06:00:00  0.000779\n2023-01-01 07:00:00  0.992212\n2023-01-01 08:00:00  0.617482\n2023-01-01 09:00:00  0.611653\n2023-01-01 10:00:00  0.007066\n2023-01-01 11:00:00  0.023062\n2023-01-01 12:00:00  0.524775\n2023-01-01 13:00:00  0.399861\n2023-01-01 14:00:00  0.046666\n2023-01-01 15:00:00  0.973756\n2023-01-01 16:00:00  0.232771\n2023-01-01 17:00:00  0.090606\n2023-01-01 18:00:00  0.618386\n2023-01-01 19:00:00  0.382462\n2023-01-01 20:00:00  0.983231\n2023-01-01 21:00:00  0.466763\n2023-01-01 22:00:00  0.859940\n2023-01-01 23:00:00  0.680308",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>value</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2023-01-01 00:00:00</th>\n      <td>0.333709</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 01:00:00</th>\n      <td>0.142867</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 02:00:00</th>\n      <td>0.650888</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 03:00:00</th>\n      <td>0.056412</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 04:00:00</th>\n      <td>0.721999</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 05:00:00</th>\n      <td>0.938553</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 06:00:00</th>\n      <td>0.000779</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 07:00:00</th>\n      <td>0.992212</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 08:00:00</th>\n      <td>0.617482</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 09:00:00</th>\n      <td>0.611653</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 10:00:00</th>\n      <td>0.007066</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 11:00:00</th>\n      <td>0.023062</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 12:00:00</th>\n      <td>0.524775</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 13:00:00</th>\n      <td>0.399861</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 14:00:00</th>\n      <td>0.046666</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 15:00:00</th>\n      <td>0.973756</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 16:00:00</th>\n      <td>0.232771</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 17:00:00</th>\n      <td>0.090606</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 18:00:00</th>\n      <td>0.618386</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 19:00:00</th>\n      <td>0.382462</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 20:00:00</th>\n      <td>0.983231</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 21:00:00</th>\n      <td>0.466763</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 22:00:00</th>\n      <td>0.859940</td>\n    </tr>\n    <tr>\n      <th>2023-01-01 23:00:00</th>\n      <td>0.680308</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hourly_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.333091200Z",
     "start_time": "2025-08-25T07:15:16.211160500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "9:00到12:00的数据:\n",
      "                        value\n",
      "2023-01-01 09:00:00  0.611653\n",
      "2023-01-01 10:00:00  0.007066\n",
      "2023-01-01 11:00:00  0.023062\n",
      "2023-01-01 12:00:00  0.524775\n"
     ]
    }
   ],
   "source": [
    "# 按时间段索引\n",
    "print(\"\\n9:00到12:00的数据:\")\n",
    "print(hourly_data.between_time('09:00', '12:00'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.334089300Z",
     "start_time": "2025-08-25T07:15:16.243142600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "15:30的数据:\n",
      "Empty DataFrame\n",
      "Columns: [value]\n",
      "Index: []\n"
     ]
    }
   ],
   "source": [
    "# 按特定时间点索引\n",
    "print(\"\\n15:30的数据:\")\n",
    "print(hourly_data.at_time('15:30'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.336089Z",
     "start_time": "2025-08-25T07:15:16.280121200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 创建日数据\n",
    "dates = pd.date_range('2023-01-01', periods=90, freq='D')\n",
    "daily_data = pd.DataFrame({\n",
    "    'sales': np.random.randint(100, 500, 90),\n",
    "    'visitors': np.random.randint(50, 200, 90)\n",
    "}, index=dates)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.578949900Z",
     "start_time": "2025-08-25T07:15:16.312101800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "            sales  visitors\n2023-01-01    428       180\n2023-01-02    266        50\n2023-01-03    373        54\n2023-01-04    487       191\n2023-01-05    188       152\n...           ...       ...\n2023-03-27    459       197\n2023-03-28    313       196\n2023-03-29    134       139\n2023-03-30    326       196\n2023-03-31    200       197\n\n[90 rows x 2 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>sales</th>\n      <th>visitors</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2023-01-01</th>\n      <td>428</td>\n      <td>180</td>\n    </tr>\n    <tr>\n      <th>2023-01-02</th>\n      <td>266</td>\n      <td>50</td>\n    </tr>\n    <tr>\n      <th>2023-01-03</th>\n      <td>373</td>\n      <td>54</td>\n    </tr>\n    <tr>\n      <th>2023-01-04</th>\n      <td>487</td>\n      <td>191</td>\n    </tr>\n    <tr>\n      <th>2023-01-05</th>\n      <td>188</td>\n      <td>152</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2023-03-27</th>\n      <td>459</td>\n      <td>197</td>\n    </tr>\n    <tr>\n      <th>2023-03-28</th>\n      <td>313</td>\n      <td>196</td>\n    </tr>\n    <tr>\n      <th>2023-03-29</th>\n      <td>134</td>\n      <td>139</td>\n    </tr>\n    <tr>\n      <th>2023-03-30</th>\n      <td>326</td>\n      <td>196</td>\n    </tr>\n    <tr>\n      <th>2023-03-31</th>\n      <td>200</td>\n      <td>197</td>\n    </tr>\n  </tbody>\n</table>\n<p>90 rows × 2 columns</p>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "daily_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.626922700Z",
     "start_time": "2025-08-25T07:15:16.320098200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "按月重采样（求和）:\n",
      "            sales  visitors\n",
      "2023-01-31   9696      3696\n",
      "2023-02-28   9269      3790\n",
      "2023-03-31   8546      4602\n"
     ]
    }
   ],
   "source": [
    "# 按月重采样（求和）\n",
    "monthly_sales = daily_data.resample('ME').sum()\n",
    "print(\"\\n按月重采样（求和）:\")\n",
    "print(monthly_sales)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.627921200Z",
     "start_time": "2025-08-25T07:15:16.366071900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "按周重采样（平均值）:\n",
      "                 sales    visitors\n",
      "2023-01-02  347.000000  115.000000\n",
      "2023-01-09  325.857143  123.142857\n",
      "2023-01-16  363.000000  135.428571\n",
      "2023-01-23  259.285714  107.000000\n",
      "2023-01-30  317.428571  116.285714\n"
     ]
    }
   ],
   "source": [
    "# 按周重采样（平均值）\n",
    "weekly_avg = daily_data.resample('W-MON').mean()\n",
    "print(\"\\n按周重采样（平均值）:\")\n",
    "print(weekly_avg.head())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.628920800Z",
     "start_time": "2025-08-25T07:15:16.400052800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建月数据\n",
    "monthly_data = pd.DataFrame({\n",
    "    'revenue': [12000, 15000, 18000, 16000],\n",
    "}, index=pd.date_range('2023-01-01', periods=4, freq='ME'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.673893600Z",
     "start_time": "2025-08-25T07:15:16.425037400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "            revenue\n2023-01-31    12000\n2023-02-28    15000\n2023-03-31    18000\n2023-04-30    16000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>revenue</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2023-01-31</th>\n      <td>12000</td>\n    </tr>\n    <tr>\n      <th>2023-02-28</th>\n      <td>15000</td>\n    </tr>\n    <tr>\n      <th>2023-03-31</th>\n      <td>18000</td>\n    </tr>\n    <tr>\n      <th>2023-04-30</th>\n      <td>16000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "monthly_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.758847200Z",
     "start_time": "2025-08-25T07:15:16.462017300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "升采样到日频率（不填充）:\n",
      "            revenue\n",
      "2023-01-31  12000.0\n",
      "2023-02-01      NaN\n",
      "2023-02-02      NaN\n",
      "2023-02-03      NaN\n",
      "2023-02-04      NaN\n",
      "2023-02-05      NaN\n",
      "2023-02-06      NaN\n",
      "2023-02-07      NaN\n",
      "2023-02-08      NaN\n",
      "2023-02-09      NaN\n"
     ]
    }
   ],
   "source": [
    "# 升采样到日频率\n",
    "daily_upsampled = monthly_data.resample('D').asfreq()\n",
    "print(\"\\n升采样到日频率（不填充）:\")\n",
    "print(daily_upsampled.head(10))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.759844900Z",
     "start_time": "2025-08-25T07:15:16.490001300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "前向填充:\n",
      "            revenue\n",
      "2023-01-31    12000\n",
      "2023-02-01    12000\n",
      "2023-02-02    12000\n",
      "2023-02-03    12000\n",
      "2023-02-04    12000\n",
      "2023-02-05    12000\n",
      "2023-02-06    12000\n",
      "2023-02-07    12000\n",
      "2023-02-08    12000\n",
      "2023-02-09    12000\n"
     ]
    }
   ],
   "source": [
    "# 前向填充\n",
    "ffilled = monthly_data.resample('D').ffill()\n",
    "print(\"\\n前向填充:\")\n",
    "print(ffilled.head(10))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.760845500Z",
     "start_time": "2025-08-25T07:15:16.518983300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "线性插值:\n",
      "                 revenue\n",
      "2023-01-31  12000.000000\n",
      "2023-02-01  12107.142857\n",
      "2023-02-02  12214.285714\n",
      "2023-02-03  12321.428571\n",
      "2023-02-04  12428.571429\n",
      "2023-02-05  12535.714286\n",
      "2023-02-06  12642.857143\n",
      "2023-02-07  12750.000000\n",
      "2023-02-08  12857.142857\n",
      "2023-02-09  12964.285714\n"
     ]
    }
   ],
   "source": [
    "# 线性插值\n",
    "interpolated = monthly_data.resample('D').interpolate(method='linear')\n",
    "print(\"\\n线性插值:\")\n",
    "print(interpolated.head(10))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.761844800Z",
     "start_time": "2025-08-25T07:15:16.551964800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 value\n",
      "2023-01-01  500.000000\n",
      "2023-01-02  541.988910\n",
      "2023-01-03  576.216206\n",
      "2023-01-04  596.354999\n",
      "2023-01-05  598.682652\n",
      "2023-01-06  582.768900\n",
      "2023-01-07  551.555386\n",
      "2023-01-08  510.811902\n",
      "2023-01-09  468.069847\n",
      "2023-01-10  431.230054\n",
      "2023-01-11  407.102328\n",
      "2023-01-12  400.146659\n",
      "2023-01-13  411.648796\n",
      "2023-01-14  439.482578\n",
      "2023-01-15  478.502956\n",
      "2023-01-16  521.497044\n",
      "2023-01-17  560.517422\n",
      "2023-01-18  588.351204\n",
      "2023-01-19  599.853341\n",
      "2023-01-20  592.897672\n",
      "2023-01-21  568.769946\n",
      "2023-01-22  531.930153\n",
      "2023-01-23  489.188098\n",
      "2023-01-24  448.444614\n",
      "2023-01-25  417.231100\n",
      "2023-01-26  401.317348\n",
      "2023-01-27  403.645001\n",
      "2023-01-28  423.783794\n",
      "2023-01-29  458.011090\n",
      "2023-01-30  500.000000\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 创建时间序列数据\n",
    "dates = pd.date_range('2023-01-01', periods=30, freq='D')\n",
    "data = pd.DataFrame({\n",
    "    'value': np.sin(np.linspace(0, 4 * np.pi, 30)) * 100 + 500\n",
    "}, index=dates)\n",
    "print(data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.762844Z",
     "start_time": "2025-08-25T07:15:16.583946100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [],
   "source": [
    "# 7天滚动平均\n",
    "data['rolling_7d'] = data['value'].rolling(window=7).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:16.987714300Z",
     "start_time": "2025-08-25T07:15:16.600936500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "                 value  rolling_7d\n2023-01-01  500.000000         NaN\n2023-01-02  541.988910         NaN\n2023-01-03  576.216206         NaN\n2023-01-04  596.354999         NaN\n2023-01-05  598.682652         NaN\n2023-01-06  582.768900         NaN\n2023-01-07  551.555386  563.938150\n2023-01-08  510.811902  565.482708\n2023-01-09  468.069847  554.922842\n2023-01-10  431.230054  534.210534\n2023-01-11  407.102328  507.174438\n2023-01-12  400.146659  478.812154\n2023-01-13  411.648796  454.366424\n2023-01-14  439.482578  438.356023\n2023-01-15  478.502956  433.740460\n2023-01-16  521.497044  441.372916\n2023-01-17  560.517422  459.842540\n2023-01-18  588.351204  485.735237\n2023-01-19  599.853341  514.264763\n2023-01-20  592.897672  540.157460\n2023-01-21  568.769946  558.627084\n2023-01-22  531.930153  566.259540\n2023-01-23  489.188098  561.643977\n2023-01-24  448.444614  545.633576\n2023-01-25  417.231100  521.187846\n2023-01-26  401.317348  492.825562\n2023-01-27  403.645001  465.789466\n2023-01-28  423.783794  445.077158\n2023-01-29  458.011090  434.517292\n2023-01-30  500.000000  436.061850",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>value</th>\n      <th>rolling_7d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2023-01-01</th>\n      <td>500.000000</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2023-01-02</th>\n      <td>541.988910</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2023-01-03</th>\n      <td>576.216206</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2023-01-04</th>\n      <td>596.354999</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2023-01-05</th>\n      <td>598.682652</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2023-01-06</th>\n      <td>582.768900</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2023-01-07</th>\n      <td>551.555386</td>\n      <td>563.938150</td>\n    </tr>\n    <tr>\n      <th>2023-01-08</th>\n      <td>510.811902</td>\n      <td>565.482708</td>\n    </tr>\n    <tr>\n      <th>2023-01-09</th>\n      <td>468.069847</td>\n      <td>554.922842</td>\n    </tr>\n    <tr>\n      <th>2023-01-10</th>\n      <td>431.230054</td>\n      <td>534.210534</td>\n    </tr>\n    <tr>\n      <th>2023-01-11</th>\n      <td>407.102328</td>\n      <td>507.174438</td>\n    </tr>\n    <tr>\n      <th>2023-01-12</th>\n      <td>400.146659</td>\n      <td>478.812154</td>\n    </tr>\n    <tr>\n      <th>2023-01-13</th>\n      <td>411.648796</td>\n      <td>454.366424</td>\n    </tr>\n    <tr>\n      <th>2023-01-14</th>\n      <td>439.482578</td>\n      <td>438.356023</td>\n    </tr>\n    <tr>\n      <th>2023-01-15</th>\n      <td>478.502956</td>\n      <td>433.740460</td>\n    </tr>\n    <tr>\n      <th>2023-01-16</th>\n      <td>521.497044</td>\n      <td>441.372916</td>\n    </tr>\n    <tr>\n      <th>2023-01-17</th>\n      <td>560.517422</td>\n      <td>459.842540</td>\n    </tr>\n    <tr>\n      <th>2023-01-18</th>\n      <td>588.351204</td>\n      <td>485.735237</td>\n    </tr>\n    <tr>\n      <th>2023-01-19</th>\n      <td>599.853341</td>\n      <td>514.264763</td>\n    </tr>\n    <tr>\n      <th>2023-01-20</th>\n      <td>592.897672</td>\n      <td>540.157460</td>\n    </tr>\n    <tr>\n      <th>2023-01-21</th>\n      <td>568.769946</td>\n      <td>558.627084</td>\n    </tr>\n    <tr>\n      <th>2023-01-22</th>\n      <td>531.930153</td>\n      <td>566.259540</td>\n    </tr>\n    <tr>\n      <th>2023-01-23</th>\n      <td>489.188098</td>\n      <td>561.643977</td>\n    </tr>\n    <tr>\n      <th>2023-01-24</th>\n      <td>448.444614</td>\n      <td>545.633576</td>\n    </tr>\n    <tr>\n      <th>2023-01-25</th>\n      <td>417.231100</td>\n      <td>521.187846</td>\n    </tr>\n    <tr>\n      <th>2023-01-26</th>\n      <td>401.317348</td>\n      <td>492.825562</td>\n    </tr>\n    <tr>\n      <th>2023-01-27</th>\n      <td>403.645001</td>\n      <td>465.789466</td>\n    </tr>\n    <tr>\n      <th>2023-01-28</th>\n      <td>423.783794</td>\n      <td>445.077158</td>\n    </tr>\n    <tr>\n      <th>2023-01-29</th>\n      <td>458.011090</td>\n      <td>434.517292</td>\n    </tr>\n    <tr>\n      <th>2023-01-30</th>\n      <td>500.000000</td>\n      <td>436.061850</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:17.144624100Z",
     "start_time": "2025-08-25T07:15:16.632918500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "# 5天时间窗口\n",
    "data['rolling_5d_time'] = data['value'].rolling('5D').mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:17.150621Z",
     "start_time": "2025-08-25T07:15:16.673893600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "                 value  rolling_7d  rolling_5d_time\n2023-01-01  500.000000         NaN       500.000000\n2023-01-02  541.988910         NaN       520.994455\n2023-01-03  576.216206         NaN       539.401705\n2023-01-04  596.354999         NaN       553.640029\n2023-01-05  598.682652         NaN       562.648553\n2023-01-06  582.768900         NaN       579.202333\n2023-01-07  551.555386  563.938150       581.115629\n2023-01-08  510.811902  565.482708       568.034768\n2023-01-09  468.069847  554.922842       542.377737\n2023-01-10  431.230054  534.210534       508.887218\n2023-01-11  407.102328  507.174438       473.753903\n2023-01-12  400.146659  478.812154       443.472158\n2023-01-13  411.648796  454.366424       423.639537\n2023-01-14  439.482578  438.356023       417.922083\n2023-01-15  478.502956  433.740460       427.376663\n2023-01-16  521.497044  441.372916       450.255607\n2023-01-17  560.517422  459.842540       482.329759\n2023-01-18  588.351204  485.735237       517.670241\n2023-01-19  599.853341  514.264763       549.744393\n2023-01-20  592.897672  540.157460       572.623337\n2023-01-21  568.769946  558.627084       582.077917\n2023-01-22  531.930153  566.259540       576.360463\n2023-01-23  489.188098  561.643977       556.527842\n2023-01-24  448.444614  545.633576       526.246097\n2023-01-25  417.231100  521.187846       491.112782\n2023-01-26  401.317348  492.825562       457.622263\n2023-01-27  403.645001  465.789466       431.965232\n2023-01-28  423.783794  445.077158       418.884371\n2023-01-29  458.011090  434.517292       420.797667\n2023-01-30  500.000000  436.061850       437.351447",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>value</th>\n      <th>rolling_7d</th>\n      <th>rolling_5d_time</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2023-01-01</th>\n      <td>500.000000</td>\n      <td>NaN</td>\n      <td>500.000000</td>\n    </tr>\n    <tr>\n      <th>2023-01-02</th>\n      <td>541.988910</td>\n      <td>NaN</td>\n      <td>520.994455</td>\n    </tr>\n    <tr>\n      <th>2023-01-03</th>\n      <td>576.216206</td>\n      <td>NaN</td>\n      <td>539.401705</td>\n    </tr>\n    <tr>\n      <th>2023-01-04</th>\n      <td>596.354999</td>\n      <td>NaN</td>\n      <td>553.640029</td>\n    </tr>\n    <tr>\n      <th>2023-01-05</th>\n      <td>598.682652</td>\n      <td>NaN</td>\n      <td>562.648553</td>\n    </tr>\n    <tr>\n      <th>2023-01-06</th>\n      <td>582.768900</td>\n      <td>NaN</td>\n      <td>579.202333</td>\n    </tr>\n    <tr>\n      <th>2023-01-07</th>\n      <td>551.555386</td>\n      <td>563.938150</td>\n      <td>581.115629</td>\n    </tr>\n    <tr>\n      <th>2023-01-08</th>\n      <td>510.811902</td>\n      <td>565.482708</td>\n      <td>568.034768</td>\n    </tr>\n    <tr>\n      <th>2023-01-09</th>\n      <td>468.069847</td>\n      <td>554.922842</td>\n      <td>542.377737</td>\n    </tr>\n    <tr>\n      <th>2023-01-10</th>\n      <td>431.230054</td>\n      <td>534.210534</td>\n      <td>508.887218</td>\n    </tr>\n    <tr>\n      <th>2023-01-11</th>\n      <td>407.102328</td>\n      <td>507.174438</td>\n      <td>473.753903</td>\n    </tr>\n    <tr>\n      <th>2023-01-12</th>\n      <td>400.146659</td>\n      <td>478.812154</td>\n      <td>443.472158</td>\n    </tr>\n    <tr>\n      <th>2023-01-13</th>\n      <td>411.648796</td>\n      <td>454.366424</td>\n      <td>423.639537</td>\n    </tr>\n    <tr>\n      <th>2023-01-14</th>\n      <td>439.482578</td>\n      <td>438.356023</td>\n      <td>417.922083</td>\n    </tr>\n    <tr>\n      <th>2023-01-15</th>\n      <td>478.502956</td>\n      <td>433.740460</td>\n      <td>427.376663</td>\n    </tr>\n    <tr>\n      <th>2023-01-16</th>\n      <td>521.497044</td>\n      <td>441.372916</td>\n      <td>450.255607</td>\n    </tr>\n    <tr>\n      <th>2023-01-17</th>\n      <td>560.517422</td>\n      <td>459.842540</td>\n      <td>482.329759</td>\n    </tr>\n    <tr>\n      <th>2023-01-18</th>\n      <td>588.351204</td>\n      <td>485.735237</td>\n      <td>517.670241</td>\n    </tr>\n    <tr>\n      <th>2023-01-19</th>\n      <td>599.853341</td>\n      <td>514.264763</td>\n      <td>549.744393</td>\n    </tr>\n    <tr>\n      <th>2023-01-20</th>\n      <td>592.897672</td>\n      <td>540.157460</td>\n      <td>572.623337</td>\n    </tr>\n    <tr>\n      <th>2023-01-21</th>\n      <td>568.769946</td>\n      <td>558.627084</td>\n      <td>582.077917</td>\n    </tr>\n    <tr>\n      <th>2023-01-22</th>\n      <td>531.930153</td>\n      <td>566.259540</td>\n      <td>576.360463</td>\n    </tr>\n    <tr>\n      <th>2023-01-23</th>\n      <td>489.188098</td>\n      <td>561.643977</td>\n      <td>556.527842</td>\n    </tr>\n    <tr>\n      <th>2023-01-24</th>\n      <td>448.444614</td>\n      <td>545.633576</td>\n      <td>526.246097</td>\n    </tr>\n    <tr>\n      <th>2023-01-25</th>\n      <td>417.231100</td>\n      <td>521.187846</td>\n      <td>491.112782</td>\n    </tr>\n    <tr>\n      <th>2023-01-26</th>\n      <td>401.317348</td>\n      <td>492.825562</td>\n      <td>457.622263</td>\n    </tr>\n    <tr>\n      <th>2023-01-27</th>\n      <td>403.645001</td>\n      <td>465.789466</td>\n      <td>431.965232</td>\n    </tr>\n    <tr>\n      <th>2023-01-28</th>\n      <td>423.783794</td>\n      <td>445.077158</td>\n      <td>418.884371</td>\n    </tr>\n    <tr>\n      <th>2023-01-29</th>\n      <td>458.011090</td>\n      <td>434.517292</td>\n      <td>420.797667</td>\n    </tr>\n    <tr>\n      <th>2023-01-30</th>\n      <td>500.000000</td>\n      <td>436.061850</td>\n      <td>437.351447</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:17.152619600Z",
     "start_time": "2025-08-25T07:15:16.679891100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "扩展窗口操作:\n",
      "                 value  expanding_mean  expanding_max\n",
      "2023-01-01  500.000000      500.000000     500.000000\n",
      "2023-01-02  541.988910      520.994455     541.988910\n",
      "2023-01-03  576.216206      539.401705     576.216206\n",
      "2023-01-04  596.354999      553.640029     596.354999\n",
      "2023-01-05  598.682652      562.648553     598.682652\n",
      "2023-01-06  582.768900      566.001944     598.682652\n",
      "2023-01-07  551.555386      563.938150     598.682652\n",
      "2023-01-08  510.811902      557.297369     598.682652\n",
      "2023-01-09  468.069847      547.383200     598.682652\n",
      "2023-01-10  431.230054      535.767886     598.682652\n"
     ]
    }
   ],
   "source": [
    "data  # 扩展窗口计算\n",
    "data['expanding_mean'] = data['value'].expanding().mean()\n",
    "data['expanding_max'] = data['value'].expanding().max()\n",
    "print(\"\\n扩展窗口操作:\")\n",
    "print(data[['value', 'expanding_mean', 'expanding_max']].head(10))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:17.212585300Z",
     "start_time": "2025-08-25T07:15:16.711873Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "指数加权移动平均:\n",
      "                 value  ewma_span10  ewma_alpha02\n",
      "2023-01-01  500.000000   500.000000    500.000000\n",
      "2023-01-02  541.988910   523.093901    523.327172\n",
      "2023-01-03  576.216206   544.448714    545.003006\n",
      "2023-01-04  596.354999   561.549522    562.398667\n",
      "2023-01-05  598.682652   572.209434    573.192333\n",
      "2023-01-06  582.768900   574.952092    575.788115\n",
      "2023-01-07  551.555386   569.314426    569.655457\n",
      "2023-01-08  510.811902   556.004820    555.514245\n",
      "2023-01-09  468.069847   536.873247    535.314155\n",
      "2023-01-10  431.230054   514.682243    511.993274\n"
     ]
    }
   ],
   "source": [
    "# 指数加权移动平均\n",
    "data['ewma_span10'] = data['value'].ewm(span=10).mean()\n",
    "data['ewma_alpha02'] = data['value'].ewm(alpha=0.2).mean()\n",
    "print(\"\\n指数加权移动平均:\")\n",
    "print(data[['value', 'ewma_span10', 'ewma_alpha02']].head(10))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:17.286543400Z",
     "start_time": "2025-08-25T07:15:16.742854800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'matplotlib'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mModuleNotFoundError\u001B[0m                       Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[33], line 3\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01mpandas\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mas\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01mpd\u001B[39;00m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01mnumpy\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mas\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01mnp\u001B[39;00m\n\u001B[1;32m----> 3\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01mmatplotlib\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mpyplot\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mas\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01mplt\u001B[39;00m\n\u001B[0;32m      4\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01mpylab\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mimport\u001B[39;00m mpl\n",
      "\u001B[1;31mModuleNotFoundError\u001B[0m: No module named 'matplotlib'"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from pylab import mpl"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:17.383487600Z",
     "start_time": "2025-08-25T07:15:16.775836400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 引入支持中文字体\n",
    "mpl.rcParams[\"font.sans-serif\"] = [\"SimHei\"]\n",
    "mpl.rcParams[\"axes.unicode_minus\"] = False"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.847794500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 创建示例数据\n",
    "np.random.seed(42)\n",
    "df = pd.DataFrame({\n",
    "    'A': np.random.randn(100).cumsum(),\n",
    "    'B': np.random.randn(100).cumsum(),\n",
    "    'C': np.random.randn(100).cumsum()\n",
    "}, index=pd.date_range('2023-01-01', periods=100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.862785600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.873780Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 基本绘图\n",
    "df.plot(figsize=(10, 6))\n",
    "plt.title('基本折线图')\n",
    "plt.ylabel('数值')\n",
    "plt.xlabel('日期')\n",
    "plt.grid(True)\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.878776800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 多线图定制\n",
    "ax = df.plot(\n",
    "    style={\n",
    "        'A': 'g--',  # 绿色虚线\n",
    "        'B': 'ro-',  # 红色圆点实线\n",
    "        'C': 'b:'  # 蓝色点线\n",
    "    },\n",
    "    linewidth=2,\n",
    "    title='定制折线图'\n",
    ")\n",
    "ax.set_ylabel('数值')\n",
    "ax.set_xlabel('日期')\n",
    "plt.grid(True, linestyle='--', alpha=0.7)\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.884773300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 创建示例数据\n",
    "bar_data = pd.DataFrame({\n",
    "    '类别': ['苹果', '香蕉', '橙子', '葡萄', '西瓜'],\n",
    "    '销量': [120, 85, 110, 65, 95],\n",
    "    '价格': [8.5, 6.0, 7.2, 12.8, 4.5]\n",
    "})\n",
    "# 垂直柱状图\n",
    "bar_data.plot(kind='bar', x='类别', y='销量',\n",
    "              color='skyblue', figsize=(10, 6),\n",
    "              title='水果销量柱状图')\n",
    "plt.ylabel('销量')\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.890771Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 水平柱状图\n",
    "bar_data.plot(kind='barh', x='类别', y='价格',\n",
    "              color='salmon', figsize=(10, 6),\n",
    "              title='水果价格水平柱状图')\n",
    "plt.xlabel('价格(元/公斤)')\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.902763300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 创建示例数据\n",
    "sales_data = pd.DataFrame({\n",
    "    '季度': ['Q1', 'Q2', 'Q3', 'Q4'],\n",
    "    '线上': [120, 150, 180, 200],\n",
    "    '线下': [80, 95, 110, 125],\n",
    "    '批发': [200, 220, 250, 280]\n",
    "})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.906760700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "sales_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.911757800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "ax = sales_data.plot(\n",
    "    kind='bar',\n",
    "    stacked=True,\n",
    "    x='季度',\n",
    "    figsize=(10, 6),\n",
    "    title='季度销售渠道分布',\n",
    "    colormap='Set2'\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.919753300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from pylab import mpl\n",
    "\n",
    "# 引入支持中文字体\n",
    "mpl.rcParams[\"font.sans-serif\"] = [\"SimHei\"]\n",
    "mpl.rcParams[\"axes.unicode_minus\"] = False\n",
    "# 创建示例数据\n",
    "sales_data = pd.DataFrame({\n",
    "    '季度': ['Q1', 'Q2', 'Q3', 'Q4'],\n",
    "    '线上': [120, 150, 180, 200],\n",
    "    '线下': [80, 95, 110, 125],\n",
    "    '批发': [200, 220, 250, 280]\n",
    "})\n",
    "# 堆叠柱状图\n",
    "ax = sales_data.plot(\n",
    "    kind='bar',\n",
    "    stacked=True,\n",
    "    x='季度',\n",
    "    figsize=(10, 6),\n",
    "    title='季度销售渠道分布',\n",
    "    colormap='Set2'\n",
    ")\n",
    "# 添加数据标签\n",
    "for p in ax.patches:\n",
    "    width, height = p.get_width(), p.get_height()\n",
    "    x, y = p.get_xy()\n",
    "    print(p, width, height, x, y)\n",
    "    if height > 0:\n",
    "        ax.annotate(f'{height:.0f}', (x + width / 2, y + height / 2),\n",
    "                    ha='center', va='center')\n",
    "plt.ylabel('销售额(万元)')\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.929747800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from pylab import mpl\n",
    "\n",
    "# 引入支持中文字体\n",
    "mpl.rcParams[\"font.sans-serif\"] = [\"SimHei\"]\n",
    "mpl.rcParams[\"axes.unicode_minus\"] = False\n",
    "# 创建时间序列数据\n",
    "np.random.seed(42)\n",
    "dates = pd.date_range('2023-01-01', periods=365)\n",
    "ts_data = pd.DataFrame({\n",
    "    '销量': 100 + np.sin(np.linspace(0, 10 * np.pi, 365)) * 50 +\n",
    "            np.random.normal(0, 10, 365),\n",
    "    '温度': 15 + 20 * np.sin(np.linspace(0, 2 * np.pi, 365)) +\n",
    "            np.random.normal(0, 3, 365)\n",
    "}, index=dates)\n",
    "# 时间序列图\n",
    "ax = ts_data['销量'].plot(figsize=(12, 6), color='blue', label='销量')\n",
    "ax.set_ylabel('销量')\n",
    "# 双Y轴\n",
    "ax2 = ax.twinx()\n",
    "ts_data['温度'].plot(ax=ax2, color='red', linestyle='--', label='温度')\n",
    "ax2.set_ylabel('温度(℃)')\n",
    "# 添加标题和图例\n",
    "plt.title('销量与温度变化趋势')\n",
    "lines, labels = ax.get_legend_handles_labels()\n",
    "lines2, labels2 = ax2.get_legend_handles_labels()\n",
    "ax.legend(lines + lines2, labels + labels2, loc='upper left')\n",
    "# 添加网格\n",
    "ax.grid(True, linestyle='--', alpha=0.7)\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-08-25T07:15:16.934744300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "            temperature   humidity\n2023-01-01           21  71.187640\n2023-01-02           18  63.874006\n2023-01-03           27  57.833310\n2023-01-04           29  43.998997\n2023-01-05           25  58.369956",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>temperature</th>\n      <th>humidity</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2023-01-01</th>\n      <td>21</td>\n      <td>71.187640</td>\n    </tr>\n    <tr>\n      <th>2023-01-02</th>\n      <td>18</td>\n      <td>63.874006</td>\n    </tr>\n    <tr>\n      <th>2023-01-03</th>\n      <td>27</td>\n      <td>57.833310</td>\n    </tr>\n    <tr>\n      <th>2023-01-04</th>\n      <td>29</td>\n      <td>43.998997</td>\n    </tr>\n    <tr>\n      <th>2023-01-05</th>\n      <td>25</td>\n      <td>58.369956</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:15:25.045740700Z",
     "start_time": "2025-08-25T07:15:25.001764500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
